package guowenlong.golf.ui.friendlymatch;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import guowenlong.golf.R;
import java.util.ArrayList;
import java.util.List;
import me.wenlong.mixutils.mvp.BaseActivity;

/**
 * des   : 描述
 * author: Administrator
 * email : guowenlong20000@gmail.com
 * time  : 2017年04月28日 下午 4:33.
 */

public class SelectedNumberActivity extends BaseActivity {

  @BindView(R.id.tv_back)
  TextView tvBack;
  @BindView(R.id.rv_seleted_number_1)
  RecyclerView rvSeletedNumber1;
  @BindView(R.id.rv_seleted_number_2)
  RecyclerView rvSeletedNumber2;
  @BindView(R.id.rv_seleted_number_3)
  RecyclerView rvSeletedNumber3;
  SelectedNumberAdapter mAdapter1;
  SelectedNumberAdapter mAdapter2;
  SelectedNumber3Adapter mAdapter3;
  @BindView(R.id.gv_selected_number)
  GridView gvSelectedNumber;
  @BindView(R.id.tv_ok)
  TextView tvOk;
  private List<SelectedNumberEntity> mDatas1;
  private List<SelectedNumberEntity> mDatas2;
  private List<SelectedNumberEntity> mDatas3;
  private List<SelectedNumberEntity> mDatas4;
  private NumberAdapter numberAdapter;

  @Override
  protected int getContentView(Bundle savedInstanceState) {
    return R.layout.activity_selected_number;
  }

  @Override
  protected void initView(Bundle savedInstanceState) {
    mDatas1 = new ArrayList<>();
    SelectedNumberEntity a1 = new SelectedNumberEntity("A场", true);
    SelectedNumberEntity a2 = new SelectedNumberEntity("B场", false);
    SelectedNumberEntity a3 = new SelectedNumberEntity("C场", false);
    mDatas1.add(a1);
    mDatas1.add(a2);
    mDatas1.add(a3);

    SelectedNumberEntity b1 = new SelectedNumberEntity("A场", true);
    SelectedNumberEntity b2 = new SelectedNumberEntity("B场", false);
    SelectedNumberEntity b3 = new SelectedNumberEntity("C场", false);
    mDatas2 = new ArrayList<>();
    mDatas2.add(b1);
    mDatas2.add(b2);
    mDatas2.add(b3);

    mDatas3 = new ArrayList<>();

    mDatas4 = new ArrayList<>();
    SelectedNumberEntity n1 = new SelectedNumberEntity("1", true);
    SelectedNumberEntity n2 = new SelectedNumberEntity("2", false);
    SelectedNumberEntity n3 = new SelectedNumberEntity("3", false);
    SelectedNumberEntity n4 = new SelectedNumberEntity("4", false);
    SelectedNumberEntity n5 = new SelectedNumberEntity("5", false);
    SelectedNumberEntity n6 = new SelectedNumberEntity("6", false);
    SelectedNumberEntity n7 = new SelectedNumberEntity("7", false);
    SelectedNumberEntity n8 = new SelectedNumberEntity("8", false);
    SelectedNumberEntity n9 = new SelectedNumberEntity("9", false);
    mDatas4.add(n1);
    mDatas4.add(n2);
    mDatas4.add(n3);
    mDatas4.add(n4);
    mDatas4.add(n5);
    mDatas4.add(n6);
    mDatas4.add(n7);
    mDatas4.add(n8);
    mDatas4.add(n9);
  }

  private void getData3() {
    mDatas3 = new ArrayList<>();
    for (int i = 0; i < mAdapter1.getmDatas().size(); i++) {
      if (mAdapter1.getmDatas().get(i).isChecked()) {
        mDatas3.add(mAdapter1.getmDatas().get(i));
      }
    }
    for (int i = 0; i < mAdapter2.getmDatas().size(); i++) {
      if (mAdapter2.getmDatas().get(i).isChecked()) {
        SelectedNumberEntity selectedNumberEntity = new SelectedNumberEntity(
            mAdapter2.getmDatas().get(i).getName(), false);
        mDatas3.add(selectedNumberEntity);
      }
    }
    LinearLayoutManager linearLayoutManager3 = new LinearLayoutManager(this);
    linearLayoutManager3.setOrientation(LinearLayoutManager.HORIZONTAL);
    rvSeletedNumber3.setLayoutManager(linearLayoutManager3);
    mAdapter3 = new SelectedNumber3Adapter(this, mDatas3);
    rvSeletedNumber3.setAdapter(mAdapter3);
  }

  @Override
  protected void initData() {
    LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(this);
    linearLayoutManager1.setOrientation(LinearLayoutManager.HORIZONTAL);
    rvSeletedNumber1.setLayoutManager(linearLayoutManager1);
    mAdapter1 = new SelectedNumberAdapter(this, mDatas1);
    rvSeletedNumber1.setAdapter(mAdapter1);

    LinearLayoutManager linearLayoutManager2 = new LinearLayoutManager(this);
    linearLayoutManager2.setOrientation(LinearLayoutManager.HORIZONTAL);
    rvSeletedNumber2.setLayoutManager(linearLayoutManager2);
    mAdapter2 = new SelectedNumberAdapter(this, mDatas2);
    rvSeletedNumber2.setAdapter(mAdapter2);

    getData3();
    numberAdapter = new NumberAdapter(mDatas4);
    gvSelectedNumber.setAdapter(numberAdapter);
  }

  public void ok() {
    Intent intent = new Intent(SelectedNumberActivity.this, FriendlyMatchActivity.class);
    String hold_number = "";
    for (int i = 0; i < mDatas3.size(); i++) {
      if (mAdapter3.getmDatas().get(i).isChecked()) {
        if (i == 0) {
          hold_number += "前九洞";
        } else if (i == 1) {
          hold_number += "后九洞";
        }
        hold_number += " "+mDatas3.get(i).getName()+" ";
      }
    }
    for (int i = 0; i < numberAdapter.getSelectedNumberEntities().size(); i++) {
      if (numberAdapter.getSelectedNumberEntities().get(i).isChecked()) {
        hold_number += "第"+numberAdapter.getSelectedNumberEntities().get(i).getName()+"洞";
      }
    }
    intent.putExtra("hold_number", hold_number);
    setResult(RESULT_OK, intent);
    finish();
  }

  @OnClick({R.id.tv_back, R.id.tv_ok})
  public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.tv_back:
        finish();
        break;
      case R.id.tv_ok:
        ok();
        break;
    }
  }

  class SelectedNumberAdapter extends Adapter<SelectedNumberAdapter.MyViewHolder> {

    List<SelectedNumberEntity> mDatas;

    public List<SelectedNumberEntity> getmDatas() {
      return mDatas;
    }

    public void setmDatas(List<SelectedNumberEntity> mDatas) {
      this.mDatas = mDatas;
    }

    public SelectedNumberAdapter(SelectedNumberActivity selectedNumberActivity,
        List<SelectedNumberEntity> mDatas) {
      this.mDatas = mDatas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
          SelectedNumberActivity.this).inflate(R.layout.item_seleted_number, parent, false));
      return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
      holder.mTxt.setText(mDatas.get(position).getName());
      if (mDatas.get(position).isChecked()) {
        holder.mTxt.setBackgroundColor(getResources().getColor(R.color.front_green));
        holder.mTxt.setTextColor(getResources().getColor(R.color.front_white));
      } else {
        holder.mTxt.setBackgroundResource(R.drawable.bg_tv_black);
        holder.mTxt.setTextColor(getResources().getColor(R.color.black_p50));
      }
      holder.mTxt.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          if (!mDatas.get(position).isChecked()) {
            for (int i = 0; i < mDatas.size(); i++) {
              mDatas.get(i).setChecked(false);
            }
            mDatas.get(position).setChecked(true);
            getData3();
          }
          notifyDataSetChanged();
          mAdapter3.notifyDataSetChanged();
        }
      });
    }

    @Override
    public int getItemCount() {
      return mDatas.size();
    }

    class MyViewHolder extends ViewHolder {

      TextView mTxt;

      MyViewHolder(View view) {
        super(view);
        mTxt = (TextView) view.findViewById(R.id.tv_number_item);
      }
    }
  }

  class SelectedNumber3Adapter extends Adapter<SelectedNumber3Adapter.MyViewHolder> {

    List<SelectedNumberEntity> mDatas;

    public List<SelectedNumberEntity> getmDatas() {
      return mDatas;
    }

    public void setmDatas(List<SelectedNumberEntity> mDatas) {
      this.mDatas = mDatas;
    }

    public SelectedNumber3Adapter(SelectedNumberActivity selectedNumberActivity,
        List<SelectedNumberEntity> mDatas) {
      this.mDatas = mDatas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
      MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
          SelectedNumberActivity.this).inflate(R.layout.item_seleted_number, parent, false));
      return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
      holder.mTxt.setText(mDatas.get(position).getName());
      if (mDatas.get(position).isChecked()) {
        holder.mTxt.setBackgroundColor(getResources().getColor(R.color.front_green));
        holder.mTxt.setTextColor(getResources().getColor(R.color.front_white));
      } else {
        holder.mTxt.setBackgroundResource(R.drawable.bg_tv_black);
        holder.mTxt.setTextColor(getResources().getColor(R.color.black_p50));
      }
      holder.mTxt.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          if (!mDatas.get(position).isChecked()) {
            for (int i = 0; i < mDatas.size(); i++) {
              mDatas.get(i).setChecked(false);
            }
            mDatas.get(position).setChecked(true);
          }
          notifyDataSetChanged();
        }
      });
    }

    @Override
    public int getItemCount() {
      return mDatas.size();
    }

    class MyViewHolder extends ViewHolder {

      TextView mTxt;

      MyViewHolder(View view) {
        super(view);
        mTxt = (TextView) view.findViewById(R.id.tv_number_item);
      }
    }
  }

  class NumberAdapter extends BaseAdapter {

    private List<SelectedNumberEntity> selectedNumberEntities;

    public List<SelectedNumberEntity> getSelectedNumberEntities() {
      return selectedNumberEntities;
    }

    public void setSelectedNumberEntities(
        List<SelectedNumberEntity> selectedNumberEntities) {
      this.selectedNumberEntities = selectedNumberEntities;
    }

    public NumberAdapter(
        List<SelectedNumberEntity> selectedNumberEntities) {
      this.selectedNumberEntities = selectedNumberEntities;
    }

    @Override
    public int getCount() {
      return selectedNumberEntities.size();
    }

    @Override
    public Object getItem(int position) {
      return null;
    }

    @Override
    public long getItemId(int position) {
      return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
      if (convertView == null) {
        convertView = View
            .inflate(SelectedNumberActivity.this, R.layout.item_gv_selected_number, null);
      }
      final TextView tv_number = (TextView) convertView.findViewById(R.id.tv_selected_number);
      if (selectedNumberEntities.get(position).isChecked()) {
        tv_number.setBackgroundResource(R.drawable.bg_green_line);
        tv_number.setTextColor(getResources().getColor(R.color.front_white));
      } else {
        tv_number.setBackgroundResource(R.drawable.bg_black_line);
        tv_number.setTextColor(getResources().getColor(R.color.black_p50));
      }
      tv_number.setText(selectedNumberEntities.get(position).getName());
      tv_number.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          for (int i = 0; i < selectedNumberEntities.size(); i++) {
            selectedNumberEntities.get(i).setChecked(false);
          }
          selectedNumberEntities.get(position).setChecked(true);
          notifyDataSetChanged();
        }
      });
      return convertView;
    }
  }
}